﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.ComponentModel.Composition;
using Plugin_DisplayFavorites;
using KVP;
using Plugin_Interfaces;



namespace PMKB_Web.Controllers
{
    [Export(typeof(Controller))]
    public partial class DisplayFavController : Controller, DisplayList
    {
        private readonly string Connection;
        private readonly string FolderSave;
        private const string saved="saved";
        protected Favorites favorites { get; set; }
        public DisplayFavController()
              : this("","")
        {
            
        }
        protected virtual string PathFile
        {
            get
            {
                return Path.Combine(FolderSave, common.UserFind.UserName + "_fav.xml");
            }
        }
        public DisplayFavController(string connectionString, string folderSave)
            : base()
        {
            Connection = connectionString;
            FolderSave = folderSave;
        }

        public virtual ActionResult Index(int? page, int? pageSize){
            
           

            var obj = TempData[saved];
            if(obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                    ViewBag.saved=obj.ToString();
            }
            else{
                    ViewBag.saved="";
            }
            
            if (favorites == null) { favorites = new Favorites(PathFile, Connection); };
            favorites.LoadExisting();
            favorites.LoadLinks(page??1,pageSize??10,"");
            
            return View(favorites);
        }
        [HttpPost]
        public  JsonResult GetData()
        {
            try
            {
                if (favorites == null) { favorites = new Favorites(PathFile, Connection); };
                favorites.LoadExisting();
                List<KVPNew<long, string>> ret = new List<KVPNew<long, string>>(favorites.favoritesList.Count);

                foreach (var item in favorites.favoritesList)
                {
                    KVPNew<long, string> i = new KVPNew<long, string>()
                    {
                        Key = item.LinkID,
                        Value = item.LinkName,
                        AdditionalData = item.LinkUrl

                    };
                    ret.Add(i);
                }
                return Json(new { ok = true, data = ret, message = "" });
            }
            catch (Exception ex)
            {
                return Json(new { ok = false, data = "", message = ex.Message });
            }
        }
        
        [HttpPost]
        public virtual ActionResult Add(long id)
        {
            try 
            {
                
                if (favorites == null) { favorites = new Favorites(PathFile, Connection); };
                favorites.AddLink(id);

            return Json(new { ok = true, message = "" });

            }
            catch (Exception ex)
            {
                //TODO: log
                return Json(new { ok = false, message = ex.Message });
            }


        }
        [HttpPost]
        public virtual ActionResult Remove(long id)
        {
            try
            {

                if (favorites == null) { favorites = new Favorites(PathFile, Connection); };
                favorites.RemoveLink(id);

                return Json(new { ok = true, message = "" });

            }
            catch (Exception ex)
            {
                //TODO: log
                return Json(new { ok = false, message = ex.Message });
            }


        }
        public ActionResult View(long id)
        {
            return null;
        }

        public string Name
        {
            get { return "Favorites"; }
        }
    }
}
